package com.yungu.swift.demo.dt.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.mapper.IMapper;
import com.yungu.swift.base.service.impl.BaseServiceImpl;
import com.yungu.swift.demo.dt.dao.DemoDt1Mapper;
import com.yungu.swift.demo.dt.model.DemoDt1;
import com.yungu.swift.demo.dt.service.DemoDt1Service;
import com.yungu.swift.system.sys.model.dto.SysUserOperateLogDto;
import com.yungu.swift.system.sys.service.SysUserOperateLogService;
import com.yungu.swift.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class DemoDt1ServiceImpl extends BaseServiceImpl<DemoDt1> implements DemoDt1Service {
    @Autowired
    private DemoDt1Mapper demoDt1Mapper;

    @Reference
    private SysUserOperateLogService sysUserOperateLogService;

    @Override
    protected IMapper<DemoDt1> getMapper() {
        return demoDt1Mapper;
    }

    @Override
    @LcnTransaction//分布式事务注解
    @Transactional//本地事务注解
    public ResponseData<Boolean> add(DemoDt1 demoDt1) {
        //步骤1. 保存本地业务数据
        Integer c1 = demoDt1Mapper.add(demoDt1);
        //步骤2. 调用远程服务保存操作日志
        SysUserOperateLogDto sysOperationLogDto = new SysUserOperateLogDto();
        sysOperationLogDto.setUuid(StringUtils.buildUUID());
        sysOperationLogDto.setOperationUser("test");
        sysOperationLogDto.setOperationRemark("分布式事务测试");
        sysOperationLogDto.setType(1);
        ResponseData<Boolean> b2 = sysUserOperateLogService.add(sysOperationLogDto);
        if (c1 > 0 && b2.getData()) {
            return ResponseData.buildSuccessResponse("保存成功", Boolean.TRUE);
        }
        return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "保存失败", Boolean.FALSE);
    }
}
